var createError = require('http-errors');  //http错误处理模块
var express = require('express'); 
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

//路由
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var stuRouter = require('./routes/stu');
var pdtRouter = require('./routes/pdt');

var app = express();

//设置视图文件的路径
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');  //设置视图引擎为ejs，类似jsp

//日志
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//挂载路由中间件
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/stu',stuRouter);
app.use('/pdt',pdtRouter);

//获取路径中query中的参数
app.get("/car",(req,res,next)=>{
  let id=req.query.id;
  res.send("/car,id="+id);
});

//获取路径中query中的参数
app.get("/car/:id/:type",(req,res,next)=>{
  let id=req.params.id
  //获取请求路径
  res.send(req.url+",id="+id+",type="+req.params.type);
});

//http错误处理
app.use(function(req, res, next) {
  next(createError(404));
});

//错误处理
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
